<template>
    <div class="login-container">
        <div class="login-left">
            <div class="brand-content">
                <h1>AI摄影分析</h1>
                <p>智能识别照片内容，生成详细分析和建议</p>
                <div class="features">
                    <div class="feature-item">
                        <span class="feature-icon">🤖</span>
                        <span>AI智能分析，多agent协同运作</span>
                    </div>
                    <div class="feature-item">
                        <span class="feature-icon">⚡</span>
                        <span>实时流式输出</span>
                    </div>
                    <div class="feature-item">
                        <span class="feature-icon">🖼️</span>
                        <span>LightRoom参数支持</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="login-card">
            <div class="login-header">
                <h2>用户登录</h2>
            </div>

            <form @submit.prevent="handleLogin" class="login-form">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input
                            id="username"
                            v-model="loginForm.username"
                            type="text"
                            placeholder="请输入用户名"
                            :disabled="loading"
                            required
                    />
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <input
                            id="password"
                            v-model="loginForm.password"
                            type="password"
                            placeholder="请输入密码"
                            :disabled="loading"
                            required
                    />
                    <div v-if="errorMessage" class="error-message">
                        {{ errorMessage }}
                    </div>
                </div>

                <button
                        type="submit"
                        class="login-btn"
                        :disabled="loading || !isFormValid"
                >
                    {{ loading ? '登录中...' : '登录' }}
                </button>
            </form>
        </div>
    </div>
</template>

<script>
import {ref, computed} from 'vue'
import {useRouter, useRoute} from 'vue-router'
import {userApi} from '@/api'
import {tokenUtils} from '@/utils/token'

export default {
    name: 'Login',
    setup() {
        const router = useRouter()
        const route = useRoute()

        const loginForm = ref({
            username: '',
            password: ''
        })

        const loading = ref(false)
        const errorMessage = ref("")


        const isFormValid = computed(() => {
            return loginForm.value.username.trim() && loginForm.value.password.trim()
        })

        const handleLogin = async () => {
            if (!isFormValid.value) return

            loading.value = true
            errorMessage.value = ''

            try {
                const token = await userApi.login({
                    username: loginForm.value.username.trim(),
                    password: loginForm.value.password.trim()
                })

                if (token) {
                    tokenUtils.set(token)
                    const redirect = route.query.redirect || '/'
                    await router.push(redirect)
                }
            } catch (error) {
                errorMessage.value = error.message || '登录失败，请重试'
            } finally {
                loading.value = false
            }
        }

        return {
            loginForm,
            loading,
            errorMessage,
            isFormValid,
            handleLogin
        }
    }
}
</script>

<style scoped>
.login-container {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 1fr 1fr;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-card {
    background: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 60px;
    min-height: 100vh;
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    color: #333;
    font-size: 28px;
    font-weight: 600;
    margin: 0;
}

.login-form {
    width: 100%;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    color: #555;
    font-weight: 500;
    font-size: 14px;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid #e1e5e9;
    border-radius: 8px;
    font-size: 16px;
    transition: all 0.3s ease;
    box-sizing: border-box;
}

.form-group input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-group input:disabled {
    background-color: #f5f5f5;
    cursor: not-allowed;
}

.error-message {
    color: #e74c3c;
    font-size: 12px;
    margin-top: 5px;
    line-height: 1.4;
}

.login-btn {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-top: 10px;
}

.login-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.login-left {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    position: relative;
    overflow: hidden;
}

.login-left::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="1" fill="white" opacity="0.05"/><circle cx="10" cy="60" r="1" fill="white" opacity="0.05"/><circle cx="90" cy="40" r="1" fill="white" opacity="0.05"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
}

.brand-content {
    text-align: center;
    z-index: 1;
    position: relative;
    max-width: 400px;
    padding: 40px;
}

.brand-content h1 {
    font-size: 48px;
    font-weight: 700;
    margin: 0 0 16px 0;
    background: linear-gradient(45deg, #fff, #f0f8ff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.brand-content p {
    font-size: 20px;
    margin: 0 0 40px 0;
    opacity: 0.9;
    line-height: 1.6;
}

.features {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.feature-item {
    display: flex;
    align-items: center;
    gap: 16px;
    font-size: 16px;
    opacity: 0.9;
}

.feature-icon {
    font-size: 24px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    backdrop-filter: blur(10px);
}

@media (max-width: 768px) {
    .login-container {
        grid-template-columns: 1fr;
    }

    .login-left {
        display: none;
    }

    .login-card {
        min-height: 100vh;
        padding: 40px 20px;
    }

    .login-header h2 {
        font-size: 24px;
    }
}
</style>